
package com.zy.je.common.mybatis.ext.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.je.common.core.service.CommonService;
import com.zy.je.common.core.utils.CheckUtil;
import com.zy.je.common.core.utils.collect.ListUtil;
import com.zy.je.common.core.utils.lang.ObjectUtil;
import com.zy.je.common.mybatis.ext.model.base.BaseEntity;
import com.zy.je.common.mybatis.ext.service.BizService;
import com.zy.je.common.mybatis.ext.util.WrapperUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zy
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public abstract class BizServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BizService<T>, CommonService {

    public T findFirstOne(Wrapper<T> queryWrapper) {
        Page<T> page = new Page();
        page.setCurrent(1);
        page.setSize(1);

        Page<T> pageData = this.page(page, queryWrapper);
        if (CheckUtil.isBlank(pageData.getRecords())) {
            return null;
        }
        return pageData.getRecords().get(0);
    }

    @Override
    public Long bizCount(T m) {
        return super.count(WrapperUtil.toWrapper(m, super.entityClass));
    }

    @Override
    public T bizGet(Serializable id) {
        return ListUtil.get(bizBatchGet(ObjectUtil.toString(id)), 0, null);
    }

    @Override
    public IPage<T> bizPageList(T entity) {
        return super.page(entity.buildPage(), WrapperUtil.toWrapper(entity, super.entityClass));
    }

    @Override
    public String bizCreate(T entity) {
        return bizSave(entity);
    }

    @Override
    public String bizUpdate(T entity) {
        return bizSave(entity);
    }

    @Override
    public String bizSave(T entity) {
        return ListUtil.get(bizBatchSave(ListUtil.newArrayList(entity)), 0, null);
    }

    @Override
    public String bizSave(T entity, LambdaUpdateWrapper<T> update) {
        if (null == entity) {
            return null;
        }
        if (!CheckUtil.isBlank(entity.getId()) && null != update) {
            update.eq(T::getId,entity.getId());
            this.update(entity, update);
            return ObjectUtil.toString(entity.getId());
        }
        return ListUtil.get(bizBatchSave(ListUtil.newArrayList(entity)), 0, null);
    }

    @Override
    public String bizDelete(Serializable id) {
        return ListUtil.get(bizBatchDelete(ObjectUtil.toString(id)), 0, null);
    }


    @Override
    public List<T> bizBatchGet(String id) {
        return super.listByIds(ListUtil.splitStringToSet(id));
    }


    @Override
    public List<String> bizBatchSave(List<T> dataList) {
        if (super.saveOrUpdateBatch(dataList)) {
            return dataList.stream()
                    .map(it -> ObjectUtil.toString(it.getId()))
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<String> bizBatchDelete(String id) {
        List<String> idList = ListUtil.splitStringToList(id);
        if (super.removeBatchByIds(idList)) {
            return idList;
        }
        return null;
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        return this.removeBatchByIds(list, 1000);
    }
}
